package com.atguigu.customer.demo.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.customer.demo.bean.Customer;
import com.atguigu.customer.demo.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @GetMapping("/customer")   //RequestParam 常用于 条件 多值参数  非关键型参数
    public String getCustomer(@RequestParam("name") String name ,@RequestParam(value = "age",required = false,defaultValue = "99") Integer age ){
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("substr(name,1,1)", name).eq("age", age));

        return  JSON.toJSONString(customerList);
    }

    @GetMapping("/customer/{id}.html") //PathVariable 常用于 主键  关键条件  唯一
    public String getCustomerById(@PathVariable("id") String id ){

        return  "id :"+id  +" customer:"+ JSON.toJSONString(customerService.getById(id)) ;
    }

    @PostMapping("/customer") //RequestBody请求体 又叫payload  常用于接收表单提交
    public String saveCustomer(@RequestBody  Customer customer ){
        customer.setName("c1018");
      customerService.saveOrUpdate(customer);  //upsert\put  //1018写入
        customer.setName("c0925");
     customerService.saveCustomer(customer);   //0925
      return  "success";

    }

}
